package com.unknowpupil.web.base.email.service.impl;

import com.unknowpupil.web.base.email.entity.Mail;
import com.unknowpupil.web.base.email.service.MailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @Classname: MailServiceImpl
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2019/10/8 16:08
 * @Description:
 * @Version: 1.0
 */
@Service
@Slf4j
public class MailServiceImpl implements MailService {
    @Resource
    private JavaMailSender mailSender;

    @Resource
    private ConfigurableApplicationContext applicationContext;
    /**
     * 发送简单邮件
     */
    @Override
    @Async
    public void sendSimpleMail(Mail mail) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(applicationContext.getEnvironment().getProperty("spring.mail.username"));
        message.setTo(mail.getTo());
        message.setSubject(mail.getTitle());
        message.setText(mail.getContent());
        try{
            mailSender.send(message);
        }catch (Exception e){
            log.error("发送简单邮件错误",e);
        }
    }

    /**
     * 发送html格式的邮件
     * @param mail
     */
    @Override
    @Async
    public void sendHtmlMail(Mail mail) {
        MimeMessage message = mailSender.createMimeMessage();
        try{
            //创建一个multipart message
            MimeMessageHelper helper = new MimeMessageHelper(message,true);
            helper.setTo(mail.getTo());
            helper.setSubject(mail.getTitle());
            helper.setText(mail.getContent());
            helper.setFrom(applicationContext.getEnvironment().getProperty("spring.mail.username"));
            mailSender.send(message);

        }catch (Exception e){
            log.error("发送html格式邮件错误",e);

        }
    }

    /**
     * @Async 开启异步发送邮件
     * 发送代附件的邮件
     * @param mail
     */
    @Override
    @Async
    public void sendAttachmentsMail(Mail mail) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(mail.getFrom());
            helper.setTo(mail.getTo());
            helper.setSubject(mail.getTitle());
            helper.setText(mail.getContent(), true);
            FileSystemResource file = new FileSystemResource(new File(mail.getFilePath()));
            String fileName = mail.getFilePath().substring(mail.getFilePath().lastIndexOf(File.separator));
            helper.addAttachment(fileName, file);
            mailSender.send(message);

        } catch (Exception e) {
            log.error("发送带附件邮件错误", e);

        }
    }
}
